package com.tanhua.server.service;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.NearUserVo;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import com.tanhua.server.utils.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;

    public ResponseEntity saveLocation(Double longitude, Double latitude, String addrStr) {

        userLocationApi.saveLocation(UserHolder.getUserId(),longitude,latitude,addrStr);

        return ResponseEntity.ok(null);
    }

    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity search(String gender, Integer distance) {
      List<UserLocationVo> userLocationList  =  userLocationApi.search(UserHolder.getUserId(),distance);

      List<NearUserVo> list = new ArrayList<>();
        for (UserLocationVo userLocation : userLocationList) {
//        大Long -128  127之间的数据是可以直接比较  不在这个区间的就是走地址比较
            if(userLocation.getUserId().longValue() == UserHolder.getUserId().longValue()){
                continue; // 继续下一次循环
            }
//            还需要根据userId查询用户  根据性别过滤
            UserInfo userInfo = userInfoApi.findById(userLocation.getUserId());
            if(StringUtils.equals(gender,userInfo.getGender())){
                list.add(new NearUserVo(userInfo.getId(),userInfo.getAvatar(),userInfo.getNickname()))   ;
            }
        }
        return ResponseEntity.ok(list);
    }
}
